package br.com.mtg.movethegarbage.impl;

import android.graphics.RectF;
import br.com.mtg.movethegarbage.framework.GUIBase;
import br.com.mtg.movethegarbage.framework.Game;
import br.com.mtg.movethegarbage.framework.GameTouchEvent;
import br.com.mtg.movethegarbage.framework.Graphics;
import br.com.mtg.movethegarbage.game.Assets;
import br.com.mtg.movethegarbage.graphics.Sprite;
import br.com.mtg.movethegarbage.graphics.SpriteManager;

public class ToggleButton extends GUIBase
{
	Sprite buttonOnSprite;
	Sprite buttonOffSprite;
	boolean buttonActive;

	public ToggleButton(float _x, float _y, float _w, float _h)
	{
		super(_x, _y, _w, _h);
		buttonActive = false;
	}

	public ToggleButton(float _x, float _y, float f, float g, Sprite spriteOn, Sprite spriteOff)
	{
		super(_x, _y, f, g);
		buttonOnSprite = spriteOn;
		buttonOffSprite = spriteOff;
		buttonActive = true;
	}

	@Override
	public void draw(Graphics graphics)
	{
		SpriteManager spriteManager = new SpriteManager(graphics, 1);
		spriteManager.beginDraw(Assets.tex_sprites);
		if (buttonActive)
			spriteManager.drawSprite(x, y, w, h, buttonOnSprite);
		else
			spriteManager.drawSprite(x, y, w, h, buttonOffSprite);
		spriteManager.endDraw();
	}

	@Override
	public void handleEvent(Object _event, Game game)
	{
		GameTouchEvent event = (GameTouchEvent) _event;
		RectF rect = getRect();
		if (event.type == GameTouchEvent.TOUCH_DOWN)
		{
			if (rect.contains(event.touchX, event.touchY))
				buttonActive = !buttonActive;
		}
	}

	public boolean isActive()
	{
		return buttonActive;
	}

	public void setActive(boolean b)
	{
		buttonActive = b;
	}
}
